<?php
class upload_photo extends MX_Controller {
	function index($sousImage='') {
		if($sousImage!='')$sousImage=$sousImage.'/';
		$setting= $this->load->module('setting/medias');
		$medias=$setting->get_Medias();
		$this->load->helper('url');
		$name=$this->input->post('name');
		if($name=='')$name=key($_FILES);
		$config['upload_path'] = './images/'.$sousImage;
		$config['file_name'] =   url_title($name).'-'.preg_replace('/([ ])/e', 'chr(rand(97,122))', str_repeat(" ", 5));
		$config['overwrite'] = TRUE;
		$config['allowed_types'] = $medias['allowed_types'];
		$config['max_size']	= $medias['max_size'];
			
		$this->load->library('upload', $config);
		if ( ! $this->upload->do_upload('images')){
			$error = array('error' => $this->upload->display_errors());
		}else{
			$data = array('images' => $this->upload->data());
				
			//[ MAIN IMAGE ]
			$config2['image_library'] = 'gd2';
			$config2['source_image'] = $this->upload->upload_path.$this->upload->file_name;
			$config2['new_image'] = './images/'.$sousImage.$this->upload->file_name;
			$config2['maintain_ratio'] = TRUE;
			$config2['overwrite'] = TRUE;
			$config2['width'] = $medias['large_size_w'];
			$config2['height'] = $medias['large_size_h'];
			$this->load->library('image_lib',$config2);
		
			if ( !$this->image_lib->resize()){
				$error = array('error' => $this->image_lib->display_errors('', ''));
			}
			////[ THUMB IMAGE ]
			$config2['new_image'] = "./images/{$sousImage}thumbnails/".$this->upload->file_name;
			$config2['width'] = $medias['thumbnail_size_w'];
			$config2['height'] = $medias['thumbnail_size_h'];
			$this->load->library('image_lib',$config2);
			$this->image_lib->initialize($config2);
			if ( !$this->image_lib->resize()){
				$error = array('error' => $this->image_lib->display_errors('', ''));
			}

			echo "<input type='hidden' name='photo[]' value='{$this->upload->file_name}'/>";
		}
		
		
	}
}